package com.company.base.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.company.base.interceptor.AuthInterceptor;
import com.company.site.mode.wechatuser.OnlineUser;
import com.company.site.mode.wechatuser.WechatUser;

public class CommonController {

	public WechatUser getCurrentOperator(HttpServletRequest request){
		HttpSession session = request.getSession();
		OnlineUser onlineUser=(OnlineUser)session.getAttribute(AuthInterceptor.OPERATOR);
		if(onlineUser == null){
			return null;
		}
		return onlineUser.getUserInfo();
	}
	
	public Long getGroupId(HttpServletRequest request){
		HttpSession session = request.getSession();
		OnlineUser onlineUser=(OnlineUser)session.getAttribute(AuthInterceptor.OPERATOR);
		if(onlineUser == null){
			return null;
		}
		return onlineUser.getGroupid();
	}
	
	public boolean isVolunteer(HttpServletRequest request){
		return true;
	}
	
	public boolean hasLogin(HttpServletRequest request){
		WechatUser user=getCurrentOperator(request);
		if(user!=null){
			return true;
		}else{
			return false;
		}
	}
	
	public void synchronizeOnlineUser(HttpServletRequest request,WechatUser user){
		WechatUser onlineUser=getCurrentOperator(request);
		onlineUser.setAddress(user.getAddress());
		onlineUser.setAge(user.getAge());
		onlineUser.setCardType(user.getCardType());
		onlineUser.setCity(user.getCity());
		onlineUser.setCompany(user.getCompany());
		onlineUser.setDegree(user.getDegree());
		onlineUser.setSex(user.getSex());
		onlineUser.setHousehold(user.getHousehold());
		onlineUser.setIdcard(user.getIdcard());
		onlineUser.setEmail(user.getEmail());
		onlineUser.setMobile(user.getMobile());
		onlineUser.setUsername(user.getUsername());
		HttpSession session = request.getSession();
		session.setMaxInactiveInterval(30*60); //登录有效时间为30分钟
		session.setAttribute(AuthInterceptor.OPERATOR, onlineUser);
	}
}
